package org.aizuomeng.common.api;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.aizuomeng.common.constant.ReturnCode;

import java.io.Serializable;

/**
 * @email 839013329@qq.com
 * @author: LuHaoLong
 * @date: 2021/11/9
 * @time: 15:44
 * @description: 统一接口返回对象
 */
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T result;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }


    /**
     * 接口正常返回
     * @return
     */
    public static<T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ReturnCode.SUCCESS);
        return r;
    }

    /**
     * 接口正常返回
     * @param data 携带数据
     * @return
     */
    public static<T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ReturnCode.SUCCESS);
        r.setResult(data);
        return r;
    }
    /**
     * 接口正常返回
     * @param message 携带信息
     * @return
     */
    public static<T> Result<T> OK(String message) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ReturnCode.SUCCESS);
        r.setMessage(message);
        return r;
    }

    /**
     * 接口正常返回
     * @param data 携带数据
     * @param message 携带信息
     * @return
     */
    public static<T> Result<T> OK(T data,String message) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ReturnCode.SUCCESS);
        r.setMessage(message);
        r.setResult(data);
        return r;
    }



    /**
     * 错误返回
     * @param msg  错误信息
     * @return
     */
    public static Result<Object> error(String msg) {
        return error(ReturnCode.ERROR, msg);
    }


    /**
     * 自定义错误状态码
     * @param code 编码
     * @param msg 错误信息
     * @return
     */
    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

}
